/*
-----------------------------------------------------------------------------
Description: Simple text display and Queue Text output
-----------------------------------------------------------------------------
*/
//#include "Ogre.h"
//#include "OgreTextAreaOverlayElement.h"
//#include "OgreConfigFile.h"

#include "TextOutput.h"

using namespace Ogre;

OverlayManager* OgreText::olm = NULL;
OverlayContainer* OgreText::panel = NULL;
Overlay* OgreText::overlay = NULL;
TextAreaOverlayElement* OgreText::textArea = NULL;
OgreQueueText* OgreQueueText::s_instance = NULL;
OgreText** OgreQueueText::ot = NULL;

OgreText::OgreText(Ogre::String szElement)
{
		OverlayName = szElement;
		olm=OverlayManager::getSingletonPtr();
		panel=static_cast<OverlayContainer*>(olm->createOverlayElement("Panel",szElement)); //GUI
		panel->setMetricsMode(Ogre::GMM_PIXELS);
		panel->setPosition(10,10);
		panel->setDimensions(100,100);
		overlay=olm->create(szElement + "_GUI"); //GUI_OVERLAY
		overlay->add2D(panel);

		textArea=static_cast<TextAreaOverlayElement*>(olm->createOverlayElement("TextArea",szElement + "TextArea"));
		textArea->setDimensions(10,10);
		textArea->setMetricsMode(Ogre::GMM_PIXELS);
		textArea->setWidth(100);
		textArea->setHeight(100);
		textArea->setFontName("BlueHighway");
		//textArea->setFontName("myfont");
		textArea->setCharHeight(22);
		textArea->setColour(Ogre::ColourValue(186.0f,23.0f,148.0f,2.0f)); // 0.3, 0.3, 0.3 //206 0 20
		panel->addChild(textArea);
}

OgreText::~OgreText()
{
	//delete olm;
	//delete panel;
	//delete overlay;
	//delete textArea;
}
void OgreText::showTextNormal(Ogre::String szString,int x,int y) //char *szString //char *szString
{
  overlay=olm->getByName(OverlayName + "_GUI");
  textArea->setCaption(szString);
  textArea->setPosition(x,y);
  overlay->show();
}

void OgreText::showText() //char *szString
{
  overlay->show();
}

void OgreText::setColor(int Color)
{
	if(Color==1)
	{		
		textArea->setColour(Ogre::ColourValue(186.0f,23.0f,148.0f,2.0f)); // 0.3, 0.3, 0.3 //206 0 20
	}
	if(Color==2)
	{
		textArea->setColour(Ogre::ColourValue(186.0f,23.0f,248.0f,2.0f)); // 0.3, 0.3, 0.3 //206 0 20
	}
}

void OgreText::setText(Ogre::String szString) //char *szString
{
  overlay=olm->getByName(OverlayName + "_GUI");
  textArea->setCaption(szString);
  itemText = szString;
}

void OgreText::setTextPos(int x,int y) //char *szString
{
  this->X = x;
  this->Y = y;
  overlay=olm->getByName(OverlayName + "_GUI"); //GUI_OVERLAY
  textArea->setPosition(x,y);
  //overlay->show();
}

OgreQueueText::OgreQueueText(Ogre::String szElement,int QueItems,int timeLim)
{
	timeLimit = timeLim;
	bFirstTime = true;
	CurrentItems = 0; //no items, call Add to add text lines
	MaxQueTextItems = QueItems;
	iSpaceBetween = 30;
	ot = new OgreText*[QueItems];
	int offsetPos;
	offsetPos = 80;
	for(int i=0;i<QueItems;i++) {
		ot[i] = new OgreText(szElement + StringConverter::toString(i) );
		ot[i]->setTextPos(0,i*iSpaceBetween + offsetPos); //iSpaceBetween = Space between each message line
	}
}

OgreQueueText* OgreQueueText::instance()
{
    if (!s_instance)
      s_instance = new OgreQueueText("textQueue",25,45000); //singleton instance
    return s_instance;                                      
}

bool OgreQueueText::Add(char *szString) //simple text output, no formatting
{
	ot[CurrentItems]->setText(szString);
	CurrentItems++;

	if(CurrentItems >= MaxQueTextItems)
		Delete();
	
	ShowText();

	return true;
}

//This class act like sprintf C function, get list of arguments and format and print result to Ogre screen
bool OgreQueueText::Addf(const char *format,...) //, ...
{
	char buffer[50];

	va_list ap;
	va_start(ap, format);
	_vsnprintf(buffer, 50, format, ap);
	va_end(ap);
	buffer[50-1]=0;

	ot[CurrentItems]->setText(buffer);
	CurrentItems++;

	if(CurrentItems >= MaxQueTextItems)
		Delete();
	
	ShowText();

	return true;
}

bool OgreQueueText::Delete()
{
	for(int i=0;i<MaxQueTextItems;i++) {
		if(i!=0)
		{
			ot[i-1]->setText(ot[i]->itemText);
			ot[i]->setText("");
		}
	}

	CurrentItems--;
	bFirstTime = true;

	return true;
}

bool OgreQueueText::clearItems() {
	if(CurrentItems==0)
		return false;
	for(int i=0;i<MaxQueTextItems;i++) {
		ot[i]->setText("");
	}
}

bool OgreQueueText::ShowText()
{
	if(CurrentItems==0)
		return false;

	for(int i=0;i<MaxQueTextItems;i++) {
		CheckTime(); //delete line if time has passed
		ot[i]->showText();
	}
	return true;
}

bool OgreQueueText::CheckTime()
{
	if(CurrentItems==0)
		return false;

	static Ogre::Timer timer;
	if(bFirstTime)
	{
		currentTime = timer.getMilliseconds();
		bFirstTime = false;
	}

	if(!bFirstTime)
	{
		if (timer.getMilliseconds() > currentTime + timeLimit  )
			Delete();
	}	
return true;
}

OgreQueueText::~OgreQueueText()
{
}

